<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\SLessonType;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class LessonTypeController extends Controller {
	use HasResourceActions;

	/**
	 * Index interface.
	 *
	 * @param Content $content
	 * @return Content
	 */
	public function index(Content $content) {
		return $content
			->header('课程类型')
			->description('列表')
			->body($this->grid());
	}

	/**
	 * Show interface.
	 *
	 * @param mixed $id
	 * @param Content $content
	 * @return Content
	 */
	public function show($id, Content $content) {
		return $content
			->header('课程类型')
			->description('详情')
			->body($this->detail($id));
	}

	/**
	 * Edit interface.
	 *
	 * @param mixed $id
	 * @param Content $content
	 * @return Content
	 */
	public function edit($id, Content $content) {
		return $content
			->header('课程类型')
			->description('编辑')
			->body($this->form()->edit($id));
	}

	/**
	 * Create interface.
	 *
	 * @param Content $content
	 * @return Content
	 */
	public function create(Content $content) {
		return $content
			->header('课程类型')
			->description('新增')
			->body($this->form());
	}

	/**
	 * Make a grid builder.
	 *
	 * @return Grid
	 */
	protected function grid() {
		$grid = new Grid(new SLessonType);

		$grid->id('Id')->sortable();
		$grid->name('类型名称');
		$grid->times('时间（分）');
		$grid->ph_price('菲律宾外教收费')->display(function ($p) {
			return sprintf("%.2f", $p / 100);
		});
		$grid->ea_price('欧美外教收费')->display(function ($p) {
			return sprintf("%.2f", $p / 100);
		});
		$grid->status('状态')->using(config('svs.lesson_type_status'));
		$grid->created_at('创建时间');
		$grid->updated_at('更新时间');

		$grid->actions(function ($actions) {
			$actions->disableDelete();
			// $actions->disableEdit();
			// $actions->disableView();
		});

		$grid->disableExport();
		return $grid;
	}

	/**
	 * Make a show builder.
	 *
	 * @param mixed $id
	 * @return Show
	 */
	protected function detail($id) {
		$show = new Show(SLessonType::findOrFail($id));
		$show->panel()
			->tools(function ($tools) {
				$tools->disableDelete();
			});
		$show->id('Id');
		$show->name('类型名称');
		$show->times('时间（分）');
		$show->ph_price('菲律宾外教收费')->as(function ($p) {
			return sprintf("%.2f", $p / 100);
		});
		$show->ea_price('欧美外教收费')->as(function ($p) {
			return sprintf("%.2f", $p / 100);
		});
		// $show->status('状态')->using( config('svs.lesson_type_status') );
		$show->status('状态')->using(config('svs.lesson_type_status'));
		$show->created_at('创建时间');
		$show->updated_at('更新时间');

		return $show;
	}

	/**
	 * Make a form builder.
	 *
	 * @return Form
	 */
	protected function form() {
		$form = new Form(new SLessonType);
		$form->tools(function (Form\Tools $tools) {
			$tools->disableDelete();
		});
		$form->text('name', '类型名称');
		$form->number('times', '时间（分）')->min(1);
		$form->number('ph_price', '菲律宾外教收费（单位：分）')->min(1);
		$form->number('ea_price', '欧美外教收费（单位：分）')->min(1);

		$states = [
			'off' => ['value' => 0, 'text' => '下架', 'color' => 'danger'],
			'on' => ['value' => 1, 'text' => '上架', 'color' => 'success'],
		];

		$form->switch('status', '状态')->states($states);

		$form->footer(function ($footer) {
			$footer->disableViewCheck();
			$footer->disableEditingCheck();
			$footer->disableCreatingCheck();
		});
		return $form;
	}
}
